<?php

namespace App\DesignPatterns\Strategy;

use App\Models\User;
use App\Models\Recharge;
use App\Models\Currency;
use App\Models\Property;
use App\Models\Message;
use App\Models\PropertyInfo;
use Exception;
use DB;

/**
 *  法币交易订单完成流程
 */
class DoneHandle implements HandleInterface
{
    public function handle($uid, $rid)
    {
        try {
            $status = 0;
            $user = User::find($uid);
            $rech = Recharge::find($rid);
            $cid = Currency::where('name_en', 'USDT')->value('id'); //USDT id
            //完成订单必须是已付款订单（状态为1）
            if ($rech->status != 2) {
                //订单信息不匹配
                throw new Exception(3004);
            }
            DB::beginTransaction();
            //卖出usdt
            if ($rech->type == 2) {
                if ($rech->rech_order->type != 1 || $rech->uid != $uid) {
                    throw new Exception(3004);
                }
                //减去用户USDT资产
                $userProperty = Property::where(['uid' => $uid, 'cid' => $cid])->first();
                $userProperty->decrement('blocked', $rech->num);//increment
                $userProperty->decrement('total', $rech->num);
                //$userProperty->in('blocked', $rech->num);
                PropertyInfo::create([
                    'uid' => $uid,
                    'cid' => $cid,
                    'oid' => $rid,
                    'note' => '用户卖出USDT',
                    'num' => $rech->num,
                    'price' => $rech->price,
                    'total' => $rech->total_price,
                    'proc' => 0,
                    'type' => 6,
                    'deal_type' => 1,
                    'status' => 1
                ]);
                //增加商户USDT资产
                $busiProperty = Property::where(['uid' => $rech->bid, 'cid' => $cid])->first();
                $busiProperty->increment('balance', $rech->num);
                $busiProperty->increment('total', $rech->num);
                PropertyInfo::create([
                    'uid' => $rech->bid,
                    'cid' => $cid,
                    'oid' => $rid,
                    'note' => '商家买入USDT',
                    'num' => $rech->num,
                    'price' => $rech->price,
                    'total' => $rech->total_price,
                    'proc' => 0,
                    'type' => 5,
                    'deal_type' => 1,
                    'status' => 1
                ]);
                //修改订单状态为已完成
                $rech->update(['status' => 3]);
            }
            //买入usdt
            if ($rech->type == 1) {
                if ($rech->rech_order->type != 2 || $rech->bid != $uid) {
                    throw new Exception(3004);
                }
                //增加用户USDT资产
                $userProperty = Property::where(['uid' => $uid, 'cid' => $cid])->first();
                $userProperty->increment('balance', $rech->num);
                $userProperty->increment('total', $rech->num);
                PropertyInfo::create([
                    'uid' => $uid,
                    'cid' => $cid,
                    'oid' => $rid,
                    'note' => '用户买入USDT',
                    'num' => $rech->num,
                    'price' => $rech->price,
                    'total' => $rech->total_price,
                    'proc' => 0,
                    'type' => 5,
                    'deal_type' => 1,
                    'status' => 1
                ]);
                //减去商户USDT资产
                $busiProperty = Property::where(['uid' => $rech->bid, 'cid' => $cid])->first();
                //$busiProperty->decrement('blocked', $rech->num);

                PropertyInfo::create([
                    'uid' => $rech->bid,
                    'cid' => $cid,
                    'oid' => $rid,
                    'note' => '商家卖出USDT',
                    'num' => $rech->num,
                    'price' => $rech->price,
                    'total' => $rech->total_price,
                    'proc' => 0,
                    'type' => 6,
                    'deal_type' => 1,
                    'status' => 1
                ]);
                //修改订单状态为已完成
                $rech->update(['status' => 3]);
                 //写入消息(确认放行人是商家，这里向用户推送消息)
                $msgArr = ['msg_id' => 10, 'uid' => $rech->uid, 'message' => [$user->nickname],"arr"=> ['number' => $rech->id]];
                (new Message())->syspublishsmsAll($msgArr);
                //这里要给商家推送消息（极光没有PC端,写入消息表）
                Message::create([
                    'admin_id' => 0,
                    'uid' => $uid,
                    'title' => '交易通知（卖出',
                    'content' => '您已经向用户'.$rech->buyer->nickname.'放行'.$rech->num.'个USDT资产，您此操作是在确认已收款的前提下进行的，平台无法控制此操作所引发的风险。',
                    'type' => 0,
                    'tpl_id' => 0
                ]);
            }

            DB::commit();

            return response()->json(['status' => 1, 'msg' => '提交成功']);

        } catch (Exception $e) {

            DB::rollback();

            $msg = $e->getMessage();

            return respF($msg);
        }
    }
}